import re


def test_search():
    pattern = r"\d{2}"
    source = "I'm 80 years 90 old."

    # 匹配一次 只要有符合的，就返回
    # 没有返回None
    result = re.search(pattern, source)
    print(result)


def test_match():
    pattern = r"\d{2}"
    source = "80 years old."

    # 匹配一次 从开头匹配，开头不符合就没有
    # 没有返回None
    result = re.match(pattern, source)
    print(result)


def test_fullmatch():
    pattern = r"\d{2}"
    source = "809"

    # 开头匹配，要完全匹配，多一个少一个都不行
    # 没有返回None
    result = re.fullmatch(pattern, source)
    print(result)


def test_findall():
    pattern = r"\d{2}"
    source = "8 09 aaa  89 laskdjf"

    # 有多少个，就返回多少个 且 结果是列表
    result = re.findall(pattern, source)
    print(result)


def test_finditer():
    pattern = r"\d{2}"
    source = "8 09 aaa  89 laskdjf"

    # iterable，可以不用一次性加载到内存，减少消耗
    it = re.finditer(pattern, source)
    for rs in it:
        print(rs)


def test_compile():
    # 预编译，适用于 要被多次使用的情况
    pattern_str = r"\d{2}"
    a = "89dhj881f"
    b = "dhd89jf"
    pattern = re.compile(pattern_str)
    print(pattern.findall(a))
    print(pattern.findall(b))


test_compile()